package com.yx.gifts.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yx.gifts.R;
import com.yx.gifts.bean.Gift;

import java.util.List;

/**
 * Created by yx on 2017/8/2.
 * 圈子顶部话题适配器
 */

public class GiftGridAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Context mContext = null;
    private List<Gift> mList = null;
    private int clickIndex = -1;//本次点击了

    public GiftGridAdapter(Context context, List< Gift> mLists) {
        mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.mList = mLists;
        this.clickIndex = -1;
    }

    public void selectItem(int positions) {
        this.clickIndex = positions;
        notifyDataSetChanged();
    }

    public List< Gift> getDataList() {
        return this.mList;
    }

    @Override
    public int getCount() {
        if (mList != null && mList.size() > 0) {
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_grid_gift, null);// 获得视图
            holder = new ViewHolder(convertView);// 初始化方法
            convertView.setTag(holder);// 设置标签使用setTag把查找的view缓存起来方便多次重用
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Glide.with(mContext)
                .load(mList.get(position).getImage())
                .into(holder.gift_iv);
        holder.itemPrice.setText(mList.get(position).getGold() != null ? mList.get(position).getGold() : "- -");
        if (clickIndex == position) {
            //选中了
            holder.item_gg_rl.setBackgroundColor(ContextCompat.getColor(mContext, R.color.white));
        } else {
            //未选中
            holder.item_gg_rl.setBackgroundColor(ContextCompat.getColor(mContext, R.color.transparent));
        }
        return convertView;
    }


    // 方法：需要设置的控件属性
    public final class ViewHolder {
        private ImageView gift_iv;
        private RelativeLayout item_gg_rl;
        private TextView itemPrice;

        public ViewHolder(View itemView) {
            gift_iv = (ImageView) itemView.findViewById(R.id.gift_iv_liwu);
            item_gg_rl = (RelativeLayout) itemView.findViewById(R.id.item_gg_rl);
            itemPrice = (TextView) itemView.findViewById(R.id.gift_tv_itemPrice);
        }
    }
}
